package com.comeval.persistencia;

import com.comeval.modelo.Factura;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Estudiante
 */
@Stateless
public class FacturaDAO implements IFacturaDAO {

    @PersistenceContext
    private EntityManager entitymanager;

    @Override
    public void registrarFactura(Factura factura) {
        try {
            entitymanager.persist(factura);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public void modificarFactura(Factura factura) {
        try {
            entitymanager.merge(factura);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public void eliminarFactura(Factura factura) {
        try {
            entitymanager.remove(factura);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public List<Factura> consultarFacturas() {
        try {
            String jpql = "Select factura From Factura factura";
            Query query = entitymanager.createQuery(jpql);
            return query.getResultList();
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public Factura consultarPorCodigo(Long codigoFactura) {
        try {
            return entitymanager.find(Factura.class, codigoFactura);
        } catch (Exception e) {
            throw e;
        }
    }

}
